package com.jack.bookshop.servlet;

import com.jack.bookshop.util.DatabaseUtil;
import com.jack.bookshop.vo.OrderBookVo;
import com.jack.bookshop.vo.OrderVo;
import com.jack.bookshop.vo.UserVo;
import jakarta.servlet.*;
import jakarta.servlet.http.*;
import jakarta.servlet.annotation.*;

import java.io.IOException;
import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@WebServlet(name = "NewOrderServlet", value = "/NewOrderServlet")
public class NewOrderServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Connection conn = null;
        PreparedStatement pst = null;
        ResultSet rs = null;
        OrderVo order = new OrderVo();
        UserVo user = (UserVo) request.getSession().getAttribute("userVo");
        order.setAddress(request.getParameter("address"));
        order.setPhone(request.getParameter("phone"));
        order.setReceiver(request.getParameter("receiver"));
//        order.setPrice(BigDecimal.valueOf(Long.parseLong(request.getParameter("price"))));
        order.setUser_id(user.getId());
        try {
            conn = DatabaseUtil.getConnection();
            String sql = "insert into demo_bookshop.order(receiver,address,phone,user_id) values (?,?,?,?)";
            pst = conn.prepareStatement(sql);
            pst.setString(1,request.getParameter("receiver"));
            pst.setString(2,request.getParameter("address"));
            pst.setString(3,request.getParameter("phone"));
//            pst.setBigDecimal(4,BigDecimal.valueOf(Long.parseLong(request.getParameter("price"))));
            pst.setInt(4,user.getId());
            pst.executeUpdate();
            response.sendRedirect("orderlist.jsp");
//            request.getSession().setAttribute("order",order);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
